package com.elinkway.infinitemovies.http.requesttask;

import android.content.Context;
import android.text.TextUtils;

import com.elinkway.infinitemovies.async.MoviesHttpAsyncTask;
import com.elinkway.infinitemovies.bean.AlbumUpdateInfoBean;
import com.elinkway.infinitemovies.bean.AlbumUpdateInfoList;
import com.elinkway.infinitemovies.dao.FavoriteDao;
import com.elinkway.infinitemovies.dao.PlayRecordDao;
import com.elinkway.infinitemovies.http.api.MoviesHttpApi;
import com.elinkway.infinitemovies.http.parser.AlbumUpdateInfoListParser;
import com.lvideo.http.bean.LVideoDataHull;

import java.util.List;

/**
 * Author: liuxiaojiang(liuxiaojiang@lvideo.com)
 * Date: 2016-06-26
 * Time: 14:32
 * Description: 请求专辑信息，主要用于更新图片
 */
public class RequestAlbumInfoTask extends MoviesHttpAsyncTask<AlbumUpdateInfoList> {
    private List<String> aidsList;
    private String type;
    @Override
    public void netNull() {
        super.netNull();
    }

    @Override
    public void netErr(int updateId, String errMsg) {
        super.netErr(updateId, errMsg);
    }

    @Override
    public void dataNull(int updateId, String errMsg) {
        super.dataNull(updateId, errMsg);
    }

    public RequestAlbumInfoTask(Context context, List<String> aidsList, String type) {
        super(context);
        this.aidsList = aidsList;
        this.type = type;
    }

    @Override
    public LVideoDataHull<AlbumUpdateInfoList> doInBackground() {
        if(aidsList != null){
            String aidString = null;
            aidString = TextUtils.join(",", aidsList);
            return MoviesHttpApi.requestAlbumUpdateInfoList(
                    new AlbumUpdateInfoListParser(aidsList), aidString);
        }else{
            return null;
        }
    }

    @Override
    public void onPostExecute(int updateId, AlbumUpdateInfoList result) {
        if(result != null){
            List<AlbumUpdateInfoBean> albumUpdateInfoList = result
                    .getAlbumUpdateInfoList();
            if(albumUpdateInfoList != null){
                if("favorite".equalsIgnoreCase(type)){
                    FavoriteDao dao = new FavoriteDao(context);
                    for (AlbumUpdateInfoBean bean : albumUpdateInfoList){
                        if(bean != null){
                            dao.updatePosters(bean);
                        }
                    }
                }else if("record".equalsIgnoreCase(type)){
                    PlayRecordDao dao = new PlayRecordDao(context);
                    for (AlbumUpdateInfoBean bean : albumUpdateInfoList){
                        if(bean != null){
                            dao.updatePosters(bean);
                        }
                    }
                }else{

                }
            }
        }
    }

}